<?php
namespace app\common\service;

use app\admin\model\Order as OrderModel;
use app\common\model\OrderTicket;
use app\common\model\User;
use think\Cache;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;
use think\Queue;

/*
 * 支付处理
 * */
class Order
{

    /*
     * 处理订单支付
     * */
    public static function handlePaidOrder($order_sn,$pay_type)
    {
        // 开启事务
        Db::startTrans();
        try {
            $OrderModel = new OrderModel();
            $order = $OrderModel->where(['status' => 0, 'order_sn' => $order_sn])->find();
            $user = User::where(['id'=>$order['user_id']])->find();
            if ($pay_type=='money'){
                //扣除余额
                $memo = "订单支付:".$order['order_sn'];
                User::money(-$order['total_fee'],$user['id'],$memo);
            }
            //更新订单
            $order->status = 1;
            $order->pay_type = $pay_type;
            $order->pay_time = time();
            $order->save();
            //更新订单票
            OrderTicket::where(['status'=>0,'order_id'=>$order['id']])->update(['status'=>1]);

            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::error('处理订单支付异常:'.$e->getMessage());
            // 抛出异常或记录日志
            throw new Exception($e->getMessage(), $e->getCode());
        }
        return true;
    }

    /*
     * 处理订单取消退款
     * */
    public static function handleCancelOrder($order_sn)
    {
        // 开启事务
        Db::startTrans();
        try {
            $OrderModel = new OrderModel();
            $order = $OrderModel->where(['order_sn' => $order_sn])->whereIn('status',[1,2])->find();
            if (!$order){
                return false;
            }
            $pay_type = $order['pay_type'];
            if ($pay_type=='money'){
                //增加余额
                $memo = "订单退款:".$order['order_sn'];
                User::money($order['total_fee'],$order['user_id'],$memo);
            }
            //更新订单
            $order->status = 3;
            $order->refund_fee = $order['total_fee'];
            $order->refund_time = time();
            $order->save();
            //更新订单票
            OrderTicket::where(['status'=>1,'order_id'=>$order['id']])->update(['status'=>3]);

            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            // 抛出异常或记录日志
            throw new Exception($e->getMessage(), $e->getCode());
        }
        return true;
    }

    /*
     * 处理订单完成退款
     * */
    public static function handleCompleteOrder($order_sn)
    {
        // 开启事务
        Db::startTrans();
        try {
            $OrderModel = new OrderModel();
            $order = $OrderModel->where(['order_sn' => $order_sn])->whereIn('status',[1])->find();
            if (!$order){
                return false;
            }
            $pay_type = $order['pay_type'];
            if ($pay_type=='money'){
                //增加余额
                $memo = "订单完成核销:".$order['order_sn'];
                User::money($order['total_fee'],$order['user_id'],$memo);
            }
            //更新订单
            $order->status = 2;
            $order->refund_fee = $order['total_fee'];
            $order->refund_time = time();
            $order->save();
            //更新订单票
            OrderTicket::where(['status'=>1,'order_id'=>$order['id']])->update(['status'=>2]);

            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            // 抛出异常或记录日志
            throw new Exception($e->getMessage(), $e->getCode());
        }
        return true;
    }
}